﻿using Newtonsoft.Json;
using SH.Common.CommonReturn;
using SH.Order.Domain.OrderClass;
using SH.Room.Application.IServices.IRoomNum;
using SH.Room.Application.IServices.IRoomState;
using SH.Room.Domain.RoomClass;
using SH.Room.Infrastructure.IRepository;
using SH.Room.Infrastructure.IRepository.Dto;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Threading.Tasks;

namespace SH.Room.Application.Services
{
    public class RoomStateServices : BaseServices<RoomState>, IRoomStateServices
    {
        private readonly IRoomStateRepository _roomStateRepository;

        private readonly IRoomTypeRepository _roomTypeRepository;

        private readonly IRoomNumRepository _roomNumRepository;

        private readonly IHttpClientFactory _httpClientFactory;

        private readonly IStayRepository _stayRepository;

        private readonly IMoreRepository _moreRepository;
        public RoomStateServices(IRoomStateRepository roomStateRepository, IRoomTypeRepository roomTypeRepository, IRoomNumRepository roomNumRepository, IStayRepository stayRepository, IHttpClientFactory httpClientFactory, IMoreRepository moreRepository) : base(roomStateRepository)
        {
            _roomStateRepository = roomStateRepository;
            _roomTypeRepository = roomTypeRepository;
            _roomNumRepository = roomNumRepository;
            _stayRepository = stayRepository;
            _httpClientFactory = httpClientFactory;
            _moreRepository = moreRepository;
        }

        private const string RoomStateUrl = "http://10.31.59.9:9033/api/ClientcuStomer/clientcuShow";

        /// <summary>
        /// 房型下拉查询
        /// </summary>
        /// <param name="rtid">房型ID</param>
        /// <returns>受影响行数</returns>
        public ApiPaging<List<RoomTypesDto>> GetRoomTypeList()
        {
            var type = _roomTypeRepository.All;
            var list = type.Select(x => new RoomTypesDto
            {
                Rtid = x.Rtid,
                Rtname = x.Rtname,
            }).ToList();
            return new ApiPaging<List<RoomTypesDto>> { Data = list };

        }

        /// <summary>
        /// 房态筛序列表显示
        /// </summary>
        /// <param name="rsid">房态筛选ID</param>
        /// <returns>受影响行数</returns>
        public ApiPaging<List<RoomState>> GetRoomStateList()
        {
            var item = _roomStateRepository.All;
            var list = item.ToList();
            return new ApiPaging<List<RoomState>> { Data = list };
        }

        /// <summary>
        /// 更多筛选列表显示
        /// </summary>
        /// <returns>受影响行数</returns>
        public ApiPaging<List<MoreScreen>> GetMoreScreenList()
        {
            return _roomStateRepository.GetMoreScreenList();
        }

        /// <summary>
        /// 修改房间状态
        /// </summary>
        /// <param name="hoteid">入住ID</param>
        /// <param name="rsid">房态ID</param>
        /// <returns>受影响行数</returns>
        public ApiResult<int> UpdState(int roomid, int Msid)
        {
            ApiResult<int> result = new ApiResult<int>();
            var num = _roomNumRepository.GetModelByCondition(x => x.RoomId == roomid);
            if (num.MsId == 1)
            {
                num.MsId = 2;
            }else if(num.MsId == 2)
            {
                num.MsId = 3;
            }
           else if(num.MsId == 3)
            {
                num.MsId = 1;
            }
            _roomNumRepository.Upd(num);
            result.Success();
            return result;
        }

        /// <summary>
        /// 房间展示列表
        /// </summary>
        /// <param name="rtid">房态ID</param>
        /// <param name="rsid">房态筛选ID</param>
        /// <param name="msid">更多筛选ID</param>
        /// <returns>受影响行数</returns>
        public ApiResult<List<RoomShowDto>> GetRoomShowList(int rtid, int rsid, int msid)
        {
            ApiResult<List<RoomShowDto>> result = new ApiResult<List<RoomShowDto>>();
            var type = _roomTypeRepository.All.ToList();
            var num = _roomNumRepository.All.ToList();
            var state = _roomStateRepository.All.ToList();
            var more = _moreRepository.All.ToList();
            //var type = _roomTypeRepository.All;
            //var num = _roomNumRepository.All;
            //var state = _roomStateRepository.All;
            //var more = _moreRepository.All;

            var item = _httpClientFactory.CreateClient();
            var items = item.GetAsync(RoomStateUrl).Result;
            if (items.IsSuccessStatusCode)
            {
                var result1 = items.Content.ReadAsStringAsync().Result;
                var info = JsonConvert.DeserializeObject<List<ClientcuStomer>>(result1);
                #region 
                //var list1 = (from a in num
                //             join b in info on a.RoomId equals b.Roomid into c
                //             from sub in c.DefaultIfEmpty()
                //             join d in type on a.Rtid equals d.Rtid
                //             //join e in state on sub.Rsid equals e.Rsid
                //             //join f in more on sub.MsId equals f.MsId
                //             where (rtid == -1 || d.Rtid == rtid)
                //             && (sub == null || (rsid == -1 || sub.Rsid == rsid))
                //             && (sub == null || (msid == -1 || sub.MsId == msid))
                //             select new RoomShowDto()
                //             {
                //                 Rtid = d.Rtid,
                //                 Rtname = d.Rtname,
                //                 RoomId = a.RoomId,
                //                 RoomNumber = a.RoomNumber,
                //                 truename = sub == null ? "" : sub.truename,
                //                 Phone = sub == null ? "" : sub.Phone,
                //                 CheckTime = sub == null ? null : sub.CheckTime,
                //                 DepartureTime = sub == null ? null : sub.DepartureTime,
                //                 NumBerday = sub == null ? 0 : sub.NumBerday,
                //             }).ToList();
                #endregion
                var list = from a in type
                           //join b in num on a.Rtid equals b.Rtid
                           select new RoomShowDto
                           {
                               Rtid = a.Rtid,
                               Rtname = a.Rtname,
                               RoomNumDtos = (from subnum in num
                                              join subinfo in info on subnum.RoomId equals subinfo.Roomid into c
                                              from sub in c.DefaultIfEmpty()
                                              where subnum.Rtid==a.Rtid
                                              select new RoomNumberDto
                                              {
                                                  RoomId = subnum.RoomId,
                                                  RoomNumber = subnum.RoomNumber,
                                                  truename = sub?.truename,
                                                  Phone = sub?.Phone,
                                                  CheckTime = sub?.CheckTime,
                                                  DepartureTime = sub?.DepartureTime,
                                                  NumBerday = sub?.NumBerday,
                                                  Rsid = sub?.Rsid,
                                                  MsId = sub?.MsId,
                                              }).ToList()
                           };
                           
                if (rtid>0)
                {
                    list = list.Where(x => x.Rtid == rtid);
                }
                if (rsid>0)
                {
                    list = list.Where(x => x.RoomNumDtos.Any(y => y.Rsid == rsid));
                }
                if (msid>0)
                {
                    list = list.Where(x => x.RoomNumDtos.Any(y => y.MsId == msid));
                }
               
               result.Success();
                result.Data = list.ToList();
                return result;
            }
            else
            {
                result.Msg = ("获取失败");
                return result;
            }
        }

        /// <summary>
        /// 房间数量统计
        /// </summary>
        /// <returns>受影响行数</returns>
        public  List<RoomCountDto> GetRoomCountList()
        {
            var item = _httpClientFactory.CreateClient();
            var items = item.GetAsync(RoomStateUrl).Result;
            if (items.IsSuccessStatusCode)
            {
                var result1 = items.Content.ReadAsStringAsync().Result;
                var list = JsonConvert.DeserializeObject<List<ClientcuStomer>>(result1);

                int AllCount = list.Count();

                int VacantCount = list.Where(x => x.Rsid == 1).Count();

                int UptownCount = list.Where(x => x.Rsid == 2).Count();

                int BookCount = list.Where(x => x.Rsid == 3).Count();

                int CheckCount = list.Where(x => x.MsId == 1).Count();

                int PigstyCount = list.Where(x => x.MsId == 2).Count();

                int MaintainCount = list.Where(x => x.MsId == 3).Count();

                return new List<RoomCountDto> { new RoomCountDto { AllCount = AllCount, VacantCount = VacantCount, UptownCount = UptownCount, BookCount = BookCount, CheckCount = CheckCount, PigstyCount = PigstyCount, MaintainCount = MaintainCount } };
            }
            else
            {
                return null;
            }
        }
    }
}
